var Users = {

    selected_row_id:null,
    objGrid:null,

    init:function () {
        var self = this;

        self.objGrid = $('#flex').flexigrid({
            url:$('#table-url').val(),
            dataType:'json',
            colModel:[
                {display:'ID', name:'id', width:20, sortable:false, align:'left', hide:true},
                {display:'Created', name:'created', width:120, sortable:false, align:'center', hide:false},
                {display:'Title', name:'title', width:250, sortable:false, align:'center', hide:false},
                {display:'Message', name:'message', width:500, sortable:false, align:'center', hide:false}
            ],
            formFields:[
                {type:'buttom', name:'New Message', bclass:'add', onpress:self.handleGridEvent},
                {separator:true}
                /*{type:'buttom', name:'Save', bclass:'save', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Edit', bclass:'edit', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Cancel', bclass:'cancel', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Delete', bclass:'delete', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Reset Password', bclass:'enable', onpress:self.handleGridEvent}*/
            ],
            /*searchitems:[
             {display:'Proforma Number', name:'invoice_number', isdefault:true}
             ],*/
            checkboxSelection:false,
            editablegrid:{
                use:false
                //url:$('#table-editable-url').val()
            },
            columnControl:false,
            sortname:"id",
            sortorder:"desc",
            usepager:true,
            useRp:true,
            rp:15,
            showTableToggleBtn:false,
            height:300,
            subgrid:{
                use:false
            }
        });

        $("#announcement_modal form").submit(function(e) {
            e.preventDefault();
            self.doAnnouncement();
        });
    },

    handleGridEvent:function (com, grid, json) {
        if (com == 'New Message') {
            Users.newAnnouncement();
        }
    },

    newAnnouncement:function(){
        $('#announcement_modal').modal({
            backdrop: 'static',
            show: true,
            keyboard: true
        });
    },

    /**
     * Triggers steps to make AJAX call to credit student account
     */
    doAnnouncement: function() {
        var self = this;
        var query = $("#announcement_modal form").serialize();
        var url = $("#announcement_modal #announcement_blast_btn").attr("href");

       // Results.disableCreditForm(true);

        $.ajax({
            type: 'post',
            url: url,
            data: query,
            dataType: 'json',
            success: function(data) {
                if(data.code == 0){
                    $("#announcement_modal").modal("hide");
                    alert(data.msg);
                    self.objGrid.flexReload();
                }
                else{
                    alert(data.msg);
                }
                //Results.disableCreditForm(false);

            },
            error: function() {
                //Results.disableCreditForm(false);
            }
        });
    }
};

/* when the page is loaded */
$(document).ready(function () {
    Users.init();
});